using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Network;
using MicroMsg.Scene;
using System;
using System.Diagnostics;
namespace MicroMsg.Manager
{
	public class ClientPerfReportMgr
	{
		private const string TAG = "ClientPerfReport";
		private PerfItemsListInfo mCachePerfItemList;
		private object lockCache = new object();
		private int mLastNewItemsNum;
		private EventWatcher mExitWatcher;
		public void appendPerfItem(uint EventID, uint ElapseTimeMs)
		{
			if (ElapseTimeMs == 0u)
			{
				return;
			}
			this.checkInitialize();
			lock (this.lockCache)
			{
				if (this.mCachePerfItemList.mListPerfItems.ContainsKey(EventID))
				{
					PerfItemInfo perfItemInfo = this.mCachePerfItemList.mListPerfItems.get_Item(EventID);
					if (ElapseTimeMs > perfItemInfo.maxElapseTime)
					{
						perfItemInfo.maxElapseTime = ElapseTimeMs;
					}
					if (ElapseTimeMs < perfItemInfo.minElapseTime)
					{
						perfItemInfo.minElapseTime = ElapseTimeMs;
					}
					perfItemInfo.avgElapseTime = (perfItemInfo.count * perfItemInfo.avgElapseTime + ElapseTimeMs) / (perfItemInfo.count + 1u);
					perfItemInfo.count += 1u;
					perfItemInfo.endTime = (uint)Util.getNowSeconds();
				}
				else
				{
					PerfItemInfo perfItemInfo = new PerfItemInfo();
					perfItemInfo.startTime = (uint)Util.getNowSeconds();
					perfItemInfo.maxElapseTime = ElapseTimeMs;
					perfItemInfo.minElapseTime = ElapseTimeMs;
					perfItemInfo.avgElapseTime = ElapseTimeMs;
					perfItemInfo.count = 1u;
					perfItemInfo.endTime = (uint)Util.getNowSeconds();
					this.mCachePerfItemList.mListPerfItems.Add(EventID, perfItemInfo);
				}
			}
			this.mLastNewItemsNum++;
			this.checkReportItem();
			this.checkFlushCache();
		}
		private void checkInitialize()
		{
			lock (this.lockCache)
			{
				if (this.mCachePerfItemList == null)
				{
					this.mCachePerfItemList = GConfigMgr.read<PerfItemsListInfo>();
					if (this.mCachePerfItemList == null)
					{
						this.mCachePerfItemList = new PerfItemsListInfo();
						this.mCachePerfItemList.mLastReportTime = (uint)Util.getNowSeconds();
					}
					this.mLastNewItemsNum = 0;
					if (this.mExitWatcher == null)
					{
						this.mExitWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onExitHandler));
						EventCenter.registerEventWatcher(EventConst.ON_PROGRAM_EXIST, this.mExitWatcher);
					}
				}
			}
		}
		private void onExitHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.mEventID != EventConst.ON_PROGRAM_EXIST)
			{
				return;
			}
			this.flushPerfItemCache();
		}
		private void checkReportItem()
		{
			lock (this.lockCache)
			{
				if (this.mCachePerfItemList != null)
				{
					if (this.mCachePerfItemList.mListPerfItems.get_Count() != 0)
					{
						uint num = (uint)Util.getNowSeconds() - this.mCachePerfItemList.mLastReportTime;
						if (num >= 14400u)
						{
							if (NetworkDeviceWatcher.isNetworkAvailable())
							{
								ServiceCenter.sceneClientPerfReport.doScene(this.mCachePerfItemList.mListPerfItems);
								this.mCachePerfItemList.mLastReportTime = (uint)Util.getNowSeconds();
								this.mCachePerfItemList.mListPerfItems.Clear();
								this.mLastNewItemsNum = 0;
								GConfigMgr.write<PerfItemsListInfo>(this.mCachePerfItemList);
							}
						}
					}
				}
			}
		}
		private void checkFlushCache()
		{
			lock (this.lockCache)
			{
				if (this.mCachePerfItemList != null)
				{
					if (this.mLastNewItemsNum != 0)
					{
						if (this.mLastNewItemsNum > 10 || Debugger.get_IsAttached())
						{
							this.mLastNewItemsNum = 0;
							GConfigMgr.write<PerfItemsListInfo>(this.mCachePerfItemList);
						}
					}
				}
			}
		}
		private void flushPerfItemCache()
		{
			lock (this.lockCache)
			{
				if (this.mCachePerfItemList != null)
				{
					if (this.mLastNewItemsNum != 0)
					{
						this.mLastNewItemsNum = 0;
						GConfigMgr.write<PerfItemsListInfo>(this.mCachePerfItemList);
					}
				}
			}
		}
	}
}
